import routes from '../route/route'
import sessionHandle from './sessionHandle'
const jwt = require('jsonwebtoken');
import '../route/playerRoute'
import '../route/rankRoute'
import '../route/gameRoute'
import app from 'src/app';

let lodash = require("lodash")

let verificationtype = {
    string: 'isString',
    number: 'isNumber',
    boolean: 'isBoolean',
    null: 'isNull',
    object: 'isObject',
    array: 'isArray',
}

function verification(msg, arr) {
    try {
        for (let item of arr) {
            let key = item[0];
            let type = item[1];
            let require = item[2];
            let value = msg[key];
            if ((value == undefined || value == null) && require == false) {
                return
            }
            if (type == 'any') {
                return
            }
            //类型验证 string ,number, boolean, null, object, array
            if (lodash[verificationtype[type]](value) == false) {
                return key;
            }
            if (type === 'number') {
                if (!Number.isInteger(value)) { //不能小于0,必须是整形
                    return key;
                }
            }
        }
    } catch (error) {
        
    }
    return
}

let routeHandle = async (url, msg, token) => {
    let validationData = routes.validationData[url];
    if (validationData) {
        let ret = verification(msg, validationData);
        if (ret) {
            return {
                err: 104,
                data: ret,
                code: 0
            }
        }
    }

    let notUserLoginRoute = routes.userNotLogin[url];
    if (notUserLoginRoute) {
        let { err, data, code } = await notUserLoginRoute(msg);
        return {
            err: err,
            data: data,
            code: code || 0
        }
    }

    let notLoginRoute = routes.notLogin[url];
    if (notLoginRoute && token) {
        let { err, data, code } = await notLoginRoute(msg, token)
        return {
            err: err,
            data: data,
            code: code || 0
        }
    }

    let loginRoute = routes.login[url];
    if (loginRoute && token) {
        let sessionKey = token.sessionKey;
        let session = await sessionHandle.getSession(sessionKey)
        if (!session) {
            return {
                err: 101,
                code: 0
            }
        }
        let forkid = app.playerSessionForksByID[session.pID]
        if (forkid == null || forkid == undefined) {
            let minAmount = app.playerSessionForks[0].amount
            let fork = null
            forkid = 0
            for (let i = 0; i < app.playerSessionForks.length; i++) {
                if (app.playerSessionForks[i].amount < minAmount) {
                    fork = app.playerSessionForks[i].fork
                    minAmount = app.playerSessionForks[i].amount
                    forkid = i
                }
            }
            app.playerSessionForks[forkid].amount += 1
        }
        app.playerSessionForksByID[session.pID] = forkid
        let fork = app.playerSessionForks[forkid].fork
        let res = await app.sendMessage(fork, url, sessionKey, msg)
        let { err, data, code } = res.data
        return {
            err: err,
            data: data,
            code: code || 0
        };
    }

    return {
        err: 103,
        data: url
    };

}


export default routeHandle